/**
 * @description: 工具类
 * @fileName: index
 * @author: luhuixu
 * @date: 2023/5/27-13:36
 * @version: V1.0.0
 **/

/**
 * @description 格式化时间
 * @param time
 * @param cFormat
 * @returns {string|null}
 */
export function parseTime(time, cFormat) {
  if (!time) return '-'
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
/**
 * @author lhx
 * @description 防抖函数
 * @param val
 * @returns {string}
 */
export function debounce(func, delay = 500) {
  let timer
  return function(...args) {
    clearTimeout(timer)
    timer = setTimeout(() => {
      func.call(this, ...args)
    }, delay)
  }
}
/**
 *  参数解析：
 * 	ctx:  canvas绘图上下文
 * 	str:  需要绘制的文本内容
 * 	draw_width:  绘制后的文字显示宽度
 * 	lineNum:  最大行数，多出部分用'...'表示， 如果传-1可以达到自动换行效果
 * 	startX:  绘制文字的起点 X 轴坐标
 * 	startY:  绘制文字的起点 Y 轴坐标
 *	steps:  文字行间距
 *	textIndent:  是否首行缩进两个字符
 */
export function toFormateStr(ctx, str, draw_width, lineNum, startX, startY, steps, textIndent) {
  var strWidth = ctx.measureText(str).width // 测量文本源尺寸信息（宽度）
  var startpoint = startY; var keyStr = ''; var sreLN = strWidth / draw_width
  // 计算文本源一共能生成多少行
  var liner = Math.ceil(sreLN)
  // 等比缩放测量一行文本显示多少个字符
  const strlen = parseInt(str.length / sreLN)
  // 若文本不足一行，则直接绘制，反之大于传入的最多行数（lineNum）以省略号（...）代替
  if (strWidth < draw_width) {
    ctx.fillText(str, startX, startpoint)
  } else {
    for (var i = 1; i < liner + 1; i++) {
      const startPoint = strlen * (i - 1)
      if (i < lineNum || lineNum === -1) {
        keyStr = str.substr(startPoint, strlen)
        ctx.fillText(keyStr, i === 1 && textIndent ? startX + draw_width / strlen * 2 : startX, startpoint)
      } else {
        keyStr = str.substr(startPoint, strlen - 5) + '...'
        ctx.fillText(keyStr, startX, startpoint)
        break
      }
      startpoint = startpoint + steps
    }
  }
}
/**
 * file转base64
 * @param { * } file 图片文件
 * @return {base64}
 */
export const fileToBase64 = file => {
  const reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onload = function(e) {
    return e.target.result
  }
}
/**
 * base64转 file
 * @param { * } dataurl 图片链接
 * filename 图片名称
 * @return {file}
 */
export const base64ImgtoFile = (dataurl, filename = 'file') => {
  const arr = dataurl.split(',')
  const mime = arr[0].match(/:(.*?);/)[1]
  const suffix = mime.split('/')[1]
  // atob : 解码使用 base-64 编码的字符串
  const bstr = window.atob(arr[1])
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], `${filename}.${suffix}`, {
    type: mime
  })
}
/**
 * 树形结构扁平化
 * @param tree 树形结构数据
 * @return []
 */
export function treeToArray(tree) {
  var res = []
  for (const item of tree) {
    const { children, ...i } = item
    if (children && children.length) {
      res = res.concat(treeToArray(children))
    }
    res.push(i)
  }
  return res
}
/**
 * @author lhx
 * @description 返回树形数据
 * @param val
 * @returns {string}
 */
export function jsonToTree(objects, keyName, parentKeyName) {
  if (!keyName) {
    keyName = 'id'
  }
  if (!parentKeyName) {
    parentKeyName = 'parentId'
  }

  // * 先生成parent建立父子关系
  const obj = {}
  objects.forEach((item) => {
    obj[item[keyName]] = item
  })
  const parentList = []
  objects.forEach((item) => {
    // item.hasChildren = false;
    const parent = obj[item[parentKeyName]]
    if (parent) {
      // * 当前项有父节点
      parent.children = parent.children || []
      parent.children.push(item)
      // parent.hasChildren = true;
    } else {
      // * 当前项没有父节点 -> 顶层
      parentList.push(item)
    }
  })
  return parentList
}
/**
 * @author lhx
 * @description 迭代方式实现补零
 * @param val
 * @returns {string}
 */
export function zeroFill(num, length) {
  for (var len = (num + '').length; len < length; len = num.length) {
    num = '0' + num
  }
  return num
}
/**
 * @author lhx
 * @description 下标转英文字母
 * @param num
 * @returns {string}
 */
export function convertToAlphabet(num) {
  let result = ''
  if (num > -1) {
    const charCode = num % 26 + 'A'.charCodeAt(0)
    result = String.fromCharCode(charCode) + result
  }
  return result
}
/**
 * @author lhx
 * @description 秒数转为时分秒
 * @param secs
 * @returns {string}
 */
export function secondsToTime(secs) {
  let hours = Math.floor(secs / 3600)
  let minutes = Math.floor((secs - (hours * 3600)) / 60)
  let seconds = secs - (hours * 3600) - (minutes * 60)

  // 添加前导零
  hours = (hours < 10) ? '0' + hours : hours
  minutes = (minutes < 10) ? '0' + minutes : minutes
  seconds = (seconds < 10) ? '0' + seconds : seconds

  return `${hours}:${minutes}:${seconds}`
}
/**
 * @author lhx
 * @description 阿拉伯数字转中文
 * @param num
 * @returns {string}
 */
export function numberToChinese(num) {
  var chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
  if (num === 0) return chineseNums[0]
  var unitPos = 0
  var str = ''
  var needZero = false

  while (num > 0) {
    var subNum = num % 10
    if (subNum === 0) {
      if (!needZero) {
        needZero = true
        str = chineseNums[0] + str
      }
    } else {
      needZero = false
      str = (subNum === 1 && unitPos === 1 && num % 100 !== 10) ? '' : chineseNums[subNum] + str
      str = (unitPos === 1 && subNum > 0) ? '' : str // 处理"十一"的情况
      str = (subNum === 1 && unitPos === 1) ? chineseNums[10] + str : str // 处理"十"的情况
    }

    unitPos++
    num = Math.floor(num / 10)
  }

  // 如果是"零十"的情况，去掉"零"
  if (str.startsWith('零十')) {
    str = str.replace('零十', '十')
  }

  // 如果是以"零"结尾，则去掉最后一个"零"
  if (str.endsWith('零') && str !== '零') {
    str = str.slice(0, -1)
  }
  return str
}
/**
 * @author lhx
 * @description 根据类型返回对应图片名称
 * @param type
 * @returns {string}
 */
export function getSVGIconFromType(type) {
  let typeName = ''
  switch (type) {
    case 'mp4':
      typeName = 'video'
      break
    case 'avi':
      typeName = 'video'
      break
    case 'flv':
      typeName = 'video'
      break
    case 'mpg':
      typeName = 'video'
      break
    case 'wmv':
      typeName = 'video'
      break
    case 'mkv':
      typeName = 'video'
      break
    case 'mov':
      typeName = 'video'
      break
    case 'doc':
      typeName = 'doc'
      break
    case 'docx':
      typeName = 'doc'
      break
    case 'xlsx':
      typeName = 'xlsx'
      break
    case 'xls':
      typeName = 'xlsx'
      break
    case 'pdf':
      typeName = 'pdf'
      break
    case 'ppt':
      typeName = 'ppt'
      break
    case 'pptx':
      typeName = 'ppt'
      break
  }
  return typeName
}
/**
 * @author lhx
 * @description 判断文本语言格式
 * @param text
 * @returns {string}
 */
export function detectLanguage(text) {
  // 如果文本为空，返回默认语言
  if (!text) return 'ch'

  // 正则表达式匹配中文
  const chineseRegex = /[\u4e00-\u9fa5]/
  // 正则表达式匹配英文
  const englishRegex = /[a-zA-Z]/

  // 如果文本中包含中文，则返回中文
  if (chineseRegex.test(text)) {
    return 'ch'
  }
  // 如果文本中只包含英文，则返回英文
  if (englishRegex.test(text)) {
    return 'en'
  }
  // 默认返回中文
  return 'ch'
}
/**
 * @author lhx
 * @description html转文本
 * @param text
 * @returns {string}
 */
export function htmlToText(html) {
  var tempDiv = document.createElement('div')
  tempDiv.innerHTML = html
  return tempDiv.textContent || tempDiv.innerText || ''
}
